/**
 * $Revision: 1.0 $
 * $Author: Eric Yang $
 * $Date: Jan 30, 2010 11:17:22 AM $
 *
 * Author: Eric Yang
 * Date  : Jan 30, 2010 11:17:22 AM
 *
 */
package com.gooex.web.action.gbuy.order;

import java.util.List;
import org.c4j.string.StringTools;
import org.c4j.system.web.action.Protected;
import org.c4j.system.web.exception.ServiceException;

import com.gooex.domain.base.Order;
import com.gooex.domain.base.OrderCustomer;
import com.gooex.domain.organization.Address;
import com.gooex.service.base.exception.ExceptionCode;
import com.gooex.service.organization.IAddressManager;

/**
 * @author Eric Yang
 * @version 1.0
 */
public class SelectAddressAction extends BaseOrderAction implements Protected {

	private IAddressManager addressManager;

	private String orderId;

	private String customerId;

	private String addressId;

	private Order order;

	private OrderCustomer orderCustomer;

	private Address address;

	private Address selectedAddress;

	/**
	 * 选择客户订单地址
	 */
	public String selectAddress() throws Exception {
		order = orderManager.getOrder(orderId);
		if (order == null) {
			throw new ServiceException(ExceptionCode.SYSTEM_ACCESS_ERROR, "");
		}

		orderCustomer = orderManager.getOrderCustomer(order, getCurrentUser());
		selectedAddress = orderCustomer.getAddress();

		if (address != null) {
			if (StringTools.isBlankOrNull(address.getId())) {
				addressManager.saveAddress(address);
				address = null;
				return "address";
			}
		}

		if (addressId == null) {
			return INPUT;
		}
		selectedAddress = addressManager.getUserAddresses(addressId, getCurrentUser());

		if (selectedAddress == null) {
			throw new ServiceException(ExceptionCode.SYSTEM_ACCESS_ERROR, "");
		}
		orderCustomer.setAddress(selectedAddress);
		orderManager.saveOrUpdate(orderCustomer);
		return SUCCESS;
	}

	public List<Address> getAddresses() {
		return addressManager.getUserAddresses(getCurrentUser());
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public String getCustomerId() {
		return customerId;
	}

	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}

	public String getAddressId() {
		return addressId;
	}

	public void setAddressId(String addressId) {
		this.addressId = addressId;
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public OrderCustomer getOrderCustomer() {
		return orderCustomer;
	}

	public void setOrderCustomer(OrderCustomer orderCustomer) {
		this.orderCustomer = orderCustomer;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public void setAddressManager(IAddressManager addressManager) {
		this.addressManager = addressManager;
	}

	public Address getSelectedAddress() {
		return selectedAddress;
	}

	public void setSelectedAddress(Address selectedAddress) {
		this.selectedAddress = selectedAddress;
	}

}
